package com.tobie.info.util;
import lombok.Data;

import java.io.Serializable;

/**
 * Ajax 请求统一响应工具类
 */
@Data
public class ResultVo<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    //状态码
    private int code;

    //状态码描述
    private String message;

    //数据结果，泛型，可以是列表、单个对象、数字、布尔值等
    private T data;

    public ResultVo() {
    }

    public ResultVo(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> ResultVo<T> success(T data) {
        ResultVo<T> resultVo = new ResultVo<>();
        resultVo.setCode(200);
        resultVo.setMessage("OK");
        resultVo.setData(data);
        return  resultVo;
    }

    public static <T> ResultVo<T> fail(T data) {
        ResultVo<T> resultVo = new ResultVo<>();
        resultVo.setCode(403);
        resultVo.setMessage("Fail");
        resultVo.setData(data);
        return  resultVo;
    }

    public static <T> ResultVo<T> result(int code,String message,T data) {
        ResultVo<T> resultVo = new ResultVo<>();
        resultVo.setCode(code);
        resultVo.setMessage(message);
        resultVo.setData(data);
        return  resultVo;
    }
}